﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI; 
using Runda.EShop.Common.Utility;
using Runda.EShop.Bll;
using System.Collections.Specialized;
using System.Configuration;

namespace Runda.EShop.Web
{
    public class BasePage:Page
    {
        #region 属性

        public new CrmUserPrincipal User
        {
            get { return base.User as CrmUserPrincipal; }
        }

        public string CurrentAppPath
        {
            get
            {
                string appPath = Request.RawUrl.ToLower();

                if (Request.ApplicationPath.ToLower() != "/")
                {
                    appPath = appPath.Replace(Request.ApplicationPath.ToLower(), "");
                }

                if (appPath.Contains("?"))
                {
                    appPath = appPath.Substring(0, appPath.IndexOf("?"));
                }
                //return appPath.TrimStart('/');
                return "~/" + appPath.TrimStart('/');
            }
        }

        #endregion

        #region Override Methods

        protected override void OnLoad(EventArgs e)
        {
            //Form验证可不用这代码
            if (this.User == null || User.Identity.LoginUser == null)
            {
                string url = Request.ApplicationPath + "/Mock.aspx";
                url = url.Replace("//", "/");
                Response.Redirect(url);
            }
            base.OnLoad(e);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnError(EventArgs e)
        {
            base.OnError(e);

            string errUrl = Request.Url.Authority + Request.ApplicationPath + "/Mock.aspx?Error=0";
            Exception ex = Server.GetLastError() as Exception;
            if (ex is HttpException)
            {
                //webconfig配置
            }
            else
            {
                string ErrorMsg = ex.Message;
                string StackTrace = ex.StackTrace;
                LogHelper.Write(StackTrace, ErrorMsg, LogCategorySource.FileLog,0,0, System.Diagnostics.TraceEventType.Error);
                //errUrl = errUrl.Replace("//", "/");
                //errUrl = "http://" + errUrl;
                //Response.Redirect(errUrl);
            }
        }

        #endregion

        #region public methods

        #endregion

        #region Private Methods

        #endregion

        #region 加解密QueryString
        private string clientQueryString;
        private NameValueCollection queryString = new NameValueCollection();
        /// <summary>
        /// 在OnPreInit中做QueryString解密
        /// <para>继承类重载时，必须调用base.OnPreInit</para>
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            clientQueryString = Request.Url.Query;
            if (!string.IsNullOrEmpty(clientQueryString))
            {
                try
                {
                    clientQueryString = Decrypt(HttpUtility.UrlDecode(clientQueryString.Substring(1)));
                    queryString = HttpUtility.ParseQueryString(clientQueryString);
                }
                catch { }
            }
        }
        /// <summary>
        /// 获取页面的查询参数QueryString
        /// </summary>
        public new string ClientQueryString
        {
            get { return clientQueryString; }
        }
        /// <summary>
        /// 获取QueryString的集合
        /// </summary>
        public NameValueCollection QueryString
        {
            get { return queryString; }
        }
        /// <summary>
        /// 解密文本
        /// </summary>
        /// <param name="text">待解密的文本</param>
        /// <returns>解密后的文本</returns>
        protected string Decrypt(string text)
        {
            return EncryptTool.Decrypt(text, ConfigurationManager.AppSettings["EncryptKey"]);
        }
        /// <summary>
        /// 加密文本
        /// </summary>
        /// <param name="text">待加密的文本</param>
        /// <returns>加密后的文本</returns>
        protected string Encrypt(string text)
        {
            return EncryptTool.Encrypt(text, ConfigurationManager.AppSettings["EncryptKey"]);
        }
        /// <summary>
        /// 加密QueryString
        /// </summary>
        /// <param name="query">待加密的QueryString</param>
        /// <returns>加密后的QueryString</returns>
        protected string EncryptQueryString(string query)
        {
            return HttpUtility.UrlEncode(Encrypt(query));
        }
        /// <summary>
        /// 解密QueryString
        /// </summary>
        /// <param name="query">待解密的QueryString</param>
        /// <returns>解密后的QueryString</returns>
        protected string DecryptQueryString(string query)
        {
            return HttpUtility.UrlDecode(Decrypt(query));
        }
        #endregion
    }
}